﻿using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.IO;
using System.Windows.Forms;

namespace SantaSLittleIrcHelper
{
	public class CurrentClientConfig
	{

        public List<String> channelList;
        public List<String> highlightList;

        public Boolean autoUnban;
        public Boolean enableHighlights;


        public void init()
        {
            this.load();
            this.updateChannelBox();

            CheckBox x = Program.instance.GUI.getElement<CheckBox>("checkBoxAutoUnban");
            if (this.autoUnban) x.Checked = true;
            x.CheckedChanged += new EventHandler(this.updateAutoUnban);
 
        }

        public void updateAutoUnban(object s, EventArgs e)
        {
            CheckBox x = Program.instance.GUI.getElement<CheckBox>("checkBoxAutoUnban");
            this.autoUnban = x.Checked;
        }

        public void updateChannelBox() {
            Panel x = Program.instance.GUI.getElement<Panel>("channelBoxPanel");
            x.Controls.Clear();
            for (int i = 0; i < channelList.Count; i++)
            {
                TextBox temp = new TextBox();
                temp.Location = new System.Drawing.Point(10, i * 25);
                temp.Size = new System.Drawing.Size(140, 20);
                temp.Text = channelList[i];
                temp.TabIndex = (i * 2);
                temp.Name = "channelName";
                temp.TextChanged += new EventHandler(this.updateChannels);
                x.Controls.Add(temp);
                Button temp2 = new Button();
                temp2.Location = new System.Drawing.Point(155, i * 25);
                temp2.Size = new System.Drawing.Size(20, 20);
                temp2.Text = "-";
                temp2.TabIndex = (i * 2) + 1;
                temp2.Click += new EventHandler(this.removeChannel);
                x.Controls.Add(temp2);
            }
            Button temp3 = new Button();
            temp3.Location = new System.Drawing.Point(155, channelList.Count * 25);
            temp3.Size = new System.Drawing.Size(20, 20);
            temp3.Text = "+";
            temp3.Click += new EventHandler(this.addChannel);
            x.Controls.Add(temp3);
        }

        public void removeChannel(object sender, EventArgs e)
        {
            Button b;
            try
            {
                b = (Button)sender;
            }
            catch (Exception)
            {
                return;
            }
            int i = (b.TabIndex - 1) / 2;
            this.channelList.RemoveAt(i);
            this.updateChannelBox();
        }

        public void addChannel(object sender, EventArgs e)
        {
            this.channelList.Add("");
            this.updateChannelBox();
        }

        public void updateChannels(object sender, EventArgs e)
        {
            Panel x = Program.instance.GUI.getElement<Panel>("channelBoxPanel");
            Control[] x2 = x.Controls.Find("channelName", false);
            for (int i = 0; i < x2.Length; i++ )
            {
                this.channelList[i] = x2[i].Text;
            }
        }


        public void load()
        {
            XmlSerializer x = new XmlSerializer(typeof(CurrentClientConfig));
            FileStream fs = new FileStream("ccc.xml", FileMode.Open);
            CurrentClientConfig c = (CurrentClientConfig)x.Deserialize(fs);
            fs.Close();

            this.channelList = c.channelList;
            this.highlightList = c.highlightList;
            this.autoUnban = c.autoUnban;
            this.enableHighlights = c.enableHighlights;

        }

        public void save()
        {
            while (this.channelList.IndexOf(String.Empty) != -1) this.channelList.Remove(String.Empty); 
            XmlSerializer x = new XmlSerializer(typeof(CurrentClientConfig));
            TextWriter writer = new StreamWriter("ccc.xml");
            x.Serialize(writer, this);
            writer.Close();
        }

	}
}
